/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TrangSuc;

import Func.Conn;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author URUKU
 */
@WebServlet(name = "Xuly", urlPatterns = {"/Xuly"})
public class Xuly extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
         HttpSession session = request.getSession(true);
         String action = request.getParameter("act");
         if(action.equalsIgnoreCase("cart"))
         {
TrangSuc.Giohang gh=(TrangSuc.Giohang)(session.getAttribute("giohang"));

if(gh==null)

gh=new Giohang();


String hang=request.getParameter("id");
String sl=request.getParameter("sl");
int soluong=Integer.parseInt(sl);
if(soluong>0){
double gia=Tinhgia(hang);
Monhang mh=new Monhang(hang,gia,soluong);
gh.ThemMonhang(mh);
session.setAttribute("giohang", gh);

//System.out.println(mh);

}
         }
         else
 if(action.equalsIgnoreCase("update"))
         {
TrangSuc.Giohang gh=(TrangSuc.Giohang)(session.getAttribute("giohang"));

if(gh!=null)
{
String hang=request.getParameter("id");
String sl=request.getParameter("txtsl");
int soluong=Integer.parseInt(sl);
if(soluong>0){
double gia=Tinhgia(hang);
Monhang mh=new Monhang(hang,gia,soluong);
gh.CapNhatMonhang(mh);
session.setAttribute("giohang", gh);

//System.out.println(mh);
}
else
{
   gh.XoaMonHang(hang);
}
}
         }
           else
 if(action.equalsIgnoreCase("delete"))
         {
TrangSuc.Giohang gh=(TrangSuc.Giohang)(session.getAttribute("giohang"));

if(gh!=null)
{
String hang=request.getParameter("id");
gh.XoaMonHang(hang);
}
         }

         RequestDispatcher rd=request.getRequestDispatcher("cart.jsp");

rd.forward(request, response);
        } finally {            
            out.close();
        }
    }
    private double Tinhgia(String sp){

double gia1 = 0;
try{
    
Connection con = Conn.GetCon();
Statement statement = con.createStatement();
ResultSet resultSet = statement.executeQuery
 ("select gia,MaKhuyenMai from thongtinsanpham where thongtinsanpham.masp = '"+sp+"'  ");
while (resultSet.next())
{
    gia1 = (resultSet.getDouble("Gia") - (resultSet.getDouble("Gia")* resultSet.getDouble("MaKhuyenMai"))/10);
}
    }
catch (Exception e) {
            e.printStackTrace();
        }
finally{
return gia1;//mt
}
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
